package com.sgb.knowledgeshare.view.activity;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityHomeShareBinding;
import com.sgb.knowledgeshare.utils.PageChangeEvent;
import com.sgb.knowledgeshare.utils.RxBus;
import com.sgb.knowledgeshare.view.adapter.ShareCommPagerAdapter;
import com.sgb.knowledgeshare.view.fragment.ShareHomeFragment;
import com.sgb.knowledgeshare.view.fragment.ShareHomeUserFragment;
import com.swgk.core.base.AppActivity;
import com.swgk.core.base.aroute.AppRouteConstant;

import java.util.ArrayList;

import rx.functions.Action1;

/**
 * 分享界面
 */
@Route(path = AppRouteConstant.HOMESHARE_ACTIVITY)
public class HomeShareActivity extends AppActivity {
    private ArrayList<Fragment> fragments;
    private ShareCommPagerAdapter pagerAdapter;
    public static int curPage;
    private ActivityHomeShareBinding binding;

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, HomeShareActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context,int index) {
        Intent intent = new Intent();
        intent.putExtra("index",index);
        intent.setClass(context, HomeShareActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_home_share);
        fragments = new ArrayList<>();
        fragments.add(ShareHomeFragment.newInstance(getIntent().getIntExtra("index", 0)));
        fragments.add(new ShareHomeUserFragment());
        pagerAdapter = new ShareCommPagerAdapter(getSupportFragmentManager(), fragments, new String[]{"", ""});
        binding.viewpager.setAdapter(pagerAdapter);
        //点击头像切换页面
        RxBus.getDefault().toObservable(PageChangeEvent.class)
                .subscribe((Action1<PageChangeEvent>) event -> {
                    if (binding.viewpager != null) {
                        binding.viewpager.setCurrentItem(event.getPage());
                    }
                });
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                curPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        binding.viewpager.setNoScroll(true);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (curPage == 0) {
            if (fragments != null && fragments.size() > 0) {
                Fragment fragment = fragments.get(0);
                if (fragment != null) {
                    if (fragment instanceof ShareHomeFragment) {
                        ((ShareHomeFragment) fragment).onVisible();
                    }
                }
            }
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (curPage == 0) {
            Fragment fragment = fragments.get(0);
            if (fragment != null) {
                if (fragment instanceof ShareHomeFragment) {
                    ((ShareHomeFragment) fragment).onInvisible();
                }
            }
        }
    }

    public void updateViewPager(int curPos, float slideOffset) {
        binding.viewpager.setNoScroll(curPos != 0 || slideOffset != 1);
        //  binding.viewpager.setNoScroll(true);
    }

    @Override
    public void onBackPressed() {
        if (curPage > 0) {
            binding.viewpager.setCurrentItem(0, true);
        } else {
            super.onBackPressed();
        }
    }
}
